﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using WD.Framework.Utility;

namespace WD.Web.MVC.Common
{
    public class FileUpload
    {
        public string UploadPic(int picType, HttpFileCollectionBase files)
        {
            var rootDir = HttpRuntime.AppDomainAppPath + "\\FileUpload\\Images\\";        //E:\Workplace\排号\WD.Web.MVC\
            const string rootPath = "/FileUpload/Images/";

            //上传图片到临时文件夹
            var tmpName = Guid.NewGuid().ToString("N").ToUpper() + ".jpg";
            var tmpDir = rootDir + "Tmp\\";
            var isSavedSuccessfully = _UploadFile(tmpDir, tmpName, files);
            if (isSavedSuccessfully)
            {
                var tmpFilePath = tmpDir + tmpName;
                if (picType == -1 || picType == -2)
                {
                    var path = "";
                    var dir = "";
                    var fileSaveDir = "";
                    //TODO ID=-1 则为KindEditor上传的图片
                    //TODO ID=-2 则为营业执照
                    switch (picType)
                    {
                        case -1:
                            fileSaveDir = "SaveAs";
                            break;
                        case -2:
                            fileSaveDir = "Yyzz";
                            break;
                    }
                    path = rootPath + fileSaveDir + "/";
                    dir = rootDir + fileSaveDir + "\\";
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
                    File.Copy(tmpFilePath, dir + tmpName);
                    //删除临时文件
                    File.Delete(tmpFilePath);

                    return path + tmpName;

                }
                else
                {

                    //TODO 获取图片裁剪尺寸
                    var data = CommonCache.CodeDataByCache[picType];
                    var width = int.Parse(data.Data);
                    var height = int.Parse(data.Data2);
                    //TODO 生成裁剪后的图片
                    var name = Guid.NewGuid().ToString("N").ToUpper() + ".jpg";
                    var path = rootPath + picType + "/";
                    var dir = rootDir + picType + "\\";
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
                    var filePath = string.Format("{0}\\{1}", dir, name);
                    ImageUtil.MakeThumbnail(tmpFilePath, filePath, width, height, "HW", false, 100);
                    //删除临时文件
                    File.Delete(tmpFilePath);

                    return path + name;
                }
            }
            else
            {
                return "";
            }
        }


        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="dirName">保存路径</param>
        /// <returns></returns>
        public bool _UploadFile(string dirName, string fileName, HttpFileCollectionBase files)
        {
            bool isSavedSuccessfully = true;
            try
            {
                foreach (string tmpName in files)
                {
                    var file = files[tmpName];
                    //Save file content goes here
                    // fName = file.FileName;
                    if (file != null && file.ContentLength > 0)
                    {
                        if (!Directory.Exists(dirName))
                            Directory.CreateDirectory(dirName);
                        var path = string.Format("{0}\\{1}", dirName, fileName);
                        file.SaveAs(path);
                    }
                }
            }
            catch (Exception ex)
            {
                isSavedSuccessfully = false;
            }

            return isSavedSuccessfully;
        }
    }
}